import java.sql.Date;

import org.junit.Test;
import org.openqa.selenium.firefox.FirefoxDriver;


public class BaseHF2Timetaken {
	String dURL,	dilogin, dipaswd, dqinput,	dExecute, dExpectedResults,	dActualResults;

	int iWait;
	
	@Test
	public void qS1() throws Exception{
		iWait = 3000;
		//Thread.sleep(iWait);
		String[][] xTD, xTC, xTS; //I need a 2 dimensional test Data to store/read the test data
		String vKW, vXpath, vTestData;
		long timeTaken;
		Date timeStart, timeEnd;  //Date objects contain the dates 2 variables of type date
		
		FirefoxDriver myD = new FirefoxDriver();
		

	
		
		String xPath = "C:\\Priya\\Silverback 4.4\\Automation Test Cases\\HybridFramework\\BackupPolicyhf.xls";
		
		//Read the entire TC and TS
		xTC = xlreadwrite.xlRead(xPath,"TestCases");
		xTS = xlreadwrite.xlRead(xPath, "TestSteps");
		xTD = xlreadwrite.xlRead(xPath, "TestData");
		
		//Go to each row in TC and identify which TC to execute
		for (int h=1; h<xTD.length; h++){
			if (xTD[h][5].equalsIgnoreCase("Y")){ //is the test Data execution for yes
				//Then read the values of the following local variables data from each row
			dURL =xTD[h][1];
			dilogin = xTD[h][2];
			dipaswd = xTD[h][3];
			dqinput = xTD[h][4];
			
	
				for (int i = 1; i<xTC.length; i++) {   //Go to each row in TC
						if (xTC[i][3].equalsIgnoreCase("Y")) {	//go to every test step if it is y
							xTC[i][4] = "Pass";  //As soon as I identify my test case to execute, it is pass by default on the excel, it updates the excel if the test case/step fails
							timeStart = new Date(); //Returns seconds from some 1970's
							for (int j=1; j<xTS.length; j++){ 
							Thread.sleep(iWait);
							if (xTC[i][0].equalsIgnoreCase(xTS[j][1])){
								
								System.out.println("KW to execute is " + xTS[j][4]);
								Thread.sleep(iWait);
								System.out.println("xpath to execute is " + xTS[j][5]);
								System.out.println("Data to execute is " + xTS[j][6]);
					
								Thread.sleep(iWait);
					// call the corresponding KW function
					vKW = xTS[j][4] ;
					vXpath = xTS[j][5];
					vTestData = xTS[j][6];
					String vError = "Pass"; // This means Before the try is executed it is pass and if it correctly executed all through try, vError will be pass
					//Get the corresponding Test Data value based on the name if the TD variable in TS
					
			 // Function to load the TD values and load into variables. Purpose either take the hardcoded data on the sh
					vTestData = getTDValue(vTestData);
					try{
						if (vKW.equalsIgnoreCase("enterText")) {
							KW.enterText(myD, vXpath, vTestData);
						}
							if (vKW.equalsIgnoreCase("navigateBrowser")){
							KW.navigateBrowser(myD, vTestData);
							}
								if(vKW.equalsIgnoreCase("clickElement")){
								KW.clickElement(myD, vXpath);
								}
									if (vKW.equalsIgnoreCase("verifyText")){
										vError = KW.verifyText(myD, vXpath, vTestData);
										System.out.println("Test is a" + vError );
									}
									if (vKW.equalsIgnoreCase("readText")){
										vError = KW.readText(myD, vXpath);
										System.out.println("Test is a" + vError);
									}
									if (vKW.equalsIgnoreCase("clickElementbylink")){
										KW.clickElementbylink(myD, vTestData);
									}
									
									if (vKW.equalsIgnoreCase("selectList")){
										KW.selectList(myD, vXpath, vTestData);
									}
									if (vKW.equalsIgnoreCase("checkBox")){
										KW.checkBox(myD, vXpath);
									}
									if(vKW.equalsIgnoreCase("waitTime")){
										KW.waitTime(3000);
										}
						if (vError.equalsIgnoreCase("Pass")) {
						System.out.println("Pass:" + xTC[i][0] + "_" + vKW); //If it's come this far then pass
						xTS[j][8] ="Pass";
						} else {
							System.out.println("Fail:" + xTC[i][0] + "_" + vKW); //If it's come this far then pass
							xTS[j][8] ="Error happened as a function returned fail";
							xTC[i][4] = "Fail";
						}
						
					} catch(Exception myError) {
						System.out.println("Fail:" + xTC[i][0] + "_" + vKW); //Fail if the try loop  fails.
						System.out.println("Error Happened" + myError);  //prints the errors into the logs.
						xTS[j][9] ="Error Happened" + myError;
						xTC[i][4] = "Fail";
					}
					
					
						}
						timeEnd = new Date();
						timeTaken = timeEnd.getTime() - timeStart.getTime();
						xTC[i][5] = timeTaken + "secs";
					}
				}
						//Write back the test Steps with the error
			xlreadwrite.writeXL("C:/Priya/Silverback 4.4/Results/TestCase-"+xTC[i][0]+".xls", "TestCase Results", xTC );
			xlreadwrite.writeXL("C:/Priya/Silverback 4.4/Results/TestStep-"+xTC[i][0]+".xls", "TestSteps Results", xTS );
			}
		
		
	
		
	
	}
		

}
		myD.quit();
		
	}

	private String getTDValue(String vTestData) {  
	
		if (vTestData.equals("dURL")){
			return dURL;  //returns the value in it
		}
		if (vTestData.equals("dilogin")){
			return dilogin;
		}
		if (vTestData.equals("dipaswd")){
			return dipaswd;
		}
		if (vTestData.equals("dqinput")){
			return dqinput;
	
		}
		
					
		return vTestData;  //If no data found , then it takes the vTestData as is in the Test Steps sheet
	}
	public void executeKW(String vKW, String vXP, String vTD){
		
	}
}		
